package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

public class StringDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
            jedis.flushAll();
            //在此处调用测试代码
            //testMsetAndMget(jedis);
            //testAppend(jedis);
            //testSetrangeAndGetrange(jedis);
            //testSetnx(jedis);
            //testIncrDecr(jedis);
            testIncrbyDecrby(jedis);
        }
    }
    public static void testMsetAndMget(Jedis jedis) {
        jedis.mset("k1","11","k2","22","k3","33");
        List<String> list = jedis.mget("k1","k2","k3","k4");
        System.out.println("list : " + list);
    }
    public static void  testAppend(Jedis jedis) {
        System.out.println("测试 append ");
        jedis.set("k1","abc");
        String result = jedis.get("k1");
        System.out.println("result : " + result);
        jedis.append("k1","def");
        result = jedis.get("k1");
        System.out.println("result : " + result);
    }
    public static void testSetrangeAndGetrange(Jedis jedis) {
        System.out.println("测试 setrange 和 getrange");
        jedis.set("key","abcdefg");
        String result = jedis.getrange("key",0,-1);
        System.out.println("result : " + result);
        result = jedis.getrange("key",1,4);
        System.out.println("result : " + result);
        jedis.setrange("key",0,"123");
        result = jedis.getrange("key",0,-1);
        System.out.println("result : " + result);
    }
    public static void testSetnx(Jedis jedis) {
        System.out.println("测试 setnx");
        jedis.set("k1","11");
        long n = jedis.setnx("k1","11");
        System.out.println(n);
        n = jedis.setnx("k2","22");
        System.out.println(n);
        String result = jedis.get("k2");
        System.out.println(result);
    }
    public static void testIncrDecr(Jedis jedis) {
        System.out.println("测试 incr 和 decr");
        jedis.set("k1","5");
        String result = jedis.get("k1");
        System.out.println("此时result : " + result);
        jedis.decr("k1");
        result = jedis.get("k1");
        System.out.println("result - 1 : " + result);
        System.out.println("此时result : " + result);
        jedis.incr("k1");
        result = jedis.get("k1");
        System.out.println("result + 1 : " + result);
    }
    public static void testIncrbyDecrby(Jedis jedis) {
        System.out.println("测试 incr 和 decr");
        jedis.set("k1","5");
        String result = jedis.get("k1");
        System.out.println("此时result : " + result);
        jedis.decrBy("k1",5);
        result = jedis.get("k1");
        System.out.println("result - 5 : " + result);
        System.out.println("此时result : " + result);
        jedis.incrBy("k1",5);
        result = jedis.get("k1");
        System.out.println("result + 5 : " + result);
    }
}
